package site.marqstree.kotlin.usercenter.ui.activity.login

import android.os.Bundle
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import kotlinx.android.synthetic.main.activity_login.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast
import site.marqstree.kotlin.library.base.extent.enable
import site.marqstree.kotlin.library.base.ui.activity.BaseMvpActivity
import site.marqstree.kotlin.usercenter.R
import site.marqstree.kotlin.library.base.data.bean.user.UserInfo
import site.marqstree.kotlin.library.base.util.UserUtil
import site.marqstree.kotlin.library.provider.router.ProviderRouter
import site.marqstree.kotlin.usercenter.injection.component.DaggerUserComponent
import site.marqstree.kotlin.usercenter.ui.activity.register.RegisterActivity
import site.marqstree.kotlin.usercenter.ui.activity.forgetpwd.ForgetPwdActivity
import site.marqstree.kotlin.usercenter.ui.activity.userinfo.UserInfoActivity


/*
 * 项目名: KotlinMall
 * 包名: site.marqstree.kotlin.usercenter.ui.activity
 * 文件名: LoginActivity
 * 创建者: marqstree
 * 创建时间: 2020/2/22 13:22
 * 描述: TODO
 */
/*
    登录界面
 */
@Route(path = ProviderRouter.User.PATH_LOGIN)
class LoginActivity : BaseMvpActivity<LoginPresenter>(),
    LoginView, View.OnClickListener{

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)

        initView()
    }

    /*
        初始化视图
     */
    private fun initView() {

        mLoginBtn.enable(mMobileEt,{isBtnEnable()})
        mLoginBtn.enable(mPwdEt,{isBtnEnable()})

        mLoginBtn.setOnClickListener(this)
        mHeaderBar.getRightView().setOnClickListener(this)
        mForgetPwdTv.setOnClickListener(this)

    }

    /*
        Dagger注册
     */
    override fun initBusinessInjection() {
        DaggerUserComponent
            .builder()
            .activityComponent(activityComponent)
            .build()
            .inject(this)
        mPresenter.mView = this
    }

    /*
        点击事件
     */
    override fun onClick(view: View) {
        when(view.id){
            //注册按钮
            R.id.mRightTv -> {startActivity<RegisterActivity>()}
            //登陆按钮
            R.id.mLoginBtn -> {
                mPresenter
                    .login(mMobileEt.text.toString()
                            ,mPwdEt.text.toString())
            }
            //忘记密码按钮
            R.id.mForgetPwdTv ->{
                startActivity<ForgetPwdActivity>()
            }
        }
    }

    /*
        判断按钮是否可用
     */
    private fun isBtnEnable():Boolean{
        return mMobileEt.text.isNullOrEmpty().not() &&
                mPwdEt.text.isNullOrEmpty().not()
    }

    /*
        登录回调
     */
    override fun onLoginResult(result: UserInfo) {
        toast("登录成功")
        //将用户及登陆信息存入sp
        UserUtil.putUserInfo(result)

        //取出ARouter拦截器写入的下一跳地址
        //跳转到被拦截地址
        val nextPath = intent.getStringExtra(ProviderRouter.NEXT_PATH) ?: ""
        val bundle = intent.extras
        if (nextPath.isNotBlank()) {
            ARouter.getInstance()
                .build(nextPath)
                .with(bundle)
                .navigation()
        } else {
            startActivity<UserInfoActivity>()
        }
        finish()
    }
}